package com.decent.web.system.controller;

import com.decent.common.bo.MessageBean;
import com.decent.common.enums.ErrorCodeEnum;
import com.decent.web.system.dto.CreateOrderDTO;
import com.decent.web.system.dto.PayOrderDTO;
import com.decent.web.system.dto.SystemOrderDTO;
import com.decent.web.system.service.SystemOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author: decent Gjh
 * @date: 2022/10/29/15:26
 * @description:
 */
@RequestMapping("/order")
@RestController
@Slf4j
@CrossOrigin
public class SystemOrderController {
    private final SystemOrderService orderService;

    public SystemOrderController(SystemOrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 下单（创建订单）
     * @param createOrderDTO 创建信息
     * @param result result
     * @return 订单详细信息
     */
    @PostMapping
    public MessageBean createOrder(@Validated(Insert.class) CreateOrderDTO createOrderDTO, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return orderService.createOrder(createOrderDTO);
    }

    /**
     * 支付
     * @param payOrderDTO  订单信息
     * @param result result
     * @return 密钥等信息
     */
    @PostMapping("/pay")
    public MessageBean payOrder(@Validated(Insert.class) PayOrderDTO payOrderDTO, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return orderService.payOrder(payOrderDTO);
    }

    /**
     * 查找订单
     * @param order 查找条件
     * @param result result
     * @return 订单列表
     */
    @GetMapping()
    public MessageBean selectOrderList(@Validated(Select.class)SystemOrderDTO order, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return orderService.selectOrderList(order);
    }


    /**
     * 根据id查看订单详情
     * @param payOrderDTO  userid 订单id
     * @param result result
     * @return 订单信息
     */
    @GetMapping("/id")
    public MessageBean selectOrderById(@Validated(Select.class) PayOrderDTO payOrderDTO, BindingResult result){
        if (result.hasErrors()){
            return new MessageBean(ErrorCodeEnum.INVALID_PARAMS, result.getAllErrors().get(0).getDefaultMessage());
        }
        return orderService.selectOrderByUuid(payOrderDTO);
    }
}
